package com.linc.skill.screenswitch;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.widget.demo.R;

public class ScreenSwichActivity extends Activity {
    private EditText editText1;
    private EditText editText2;
    private EditText editText3;
    private Button btnGetInfo,btnSetTag,btnSetLand,btnNoSwitch;
    private static final String TEXT_ONE = "TXT_ONE";
    private static final String TEXT_TWO = "TXT_TWO";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_land_port_switch);
        Log.e("linc","oncreate");
        editText1 = (EditText)findViewById(R.id.txt1);
        editText2 = (EditText)findViewById(R.id.txt2);
        editText3 = (EditText)findViewById(R.id.txt3);
        // Restore saved state.
        if (savedInstanceState != null) {
            editText1.setTag(R.id.tag_first,savedInstanceState.getString(TEXT_ONE));
        }
        btnGetInfo = (Button)findViewById(R.id.btn_get_info);
        btnGetInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("linc","edittxt1 is "+editText1.getText()+
                    " tag is "+editText1.getTag(R.id.tag_first));
                Log.i("linc","edittxt2 is "+editText2.getText()+
                " content description is "+editText2.getContentDescription());
                Log.i("linc","edittxt3 is "+editText3.getText()+
                        " content description is "+editText3.getContentDescription());
            }
        });
        btnSetTag = (Button)findViewById(R.id.btn_set_tag);
        btnSetTag.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editText1.setTag(R.id.tag_first,"gagag");
                editText2.setContentDescription("this is edit text2");
                editText3.setContentDescription("text3 text3");
            }
        });
        btnSetLand = (Button)findViewById(R.id.btn_set_land);
        btnSetLand.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
                if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                    Toast.makeText(ScreenSwichActivity.this,"this port",Toast.LENGTH_SHORT).show();
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                }
                else {
                    Toast.makeText(ScreenSwichActivity.this, "this land", Toast.LENGTH_SHORT).show();
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                }
            }

        });
        btnNoSwitch = (Button)findViewById(R.id.btn_set_no_switch);
        btnNoSwitch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                if(getRequestedOrientation()==ActivityInfo.SCREEN_ORIENTATION_NOSENSOR)
//                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
//                else
//                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
            }
        });
        if (savedInstanceState != null) {
            editText1.setTag(R.id.tag_first,savedInstanceState.getString(TEXT_ONE));
            editText2.setContentDescription(savedInstanceState.getString(TEXT_TWO));
            editText3.setContentDescription(savedInstanceState.getString(""+editText3.getId()));
        }
    }

    //android:configChanges="orientation|keyboardHidden|screenSize"
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Log.i("linc", "onConfigurationChanged");
        if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
            Log.i("linc","land: txt1 is "+editText1.getText());
        }else{
            Log.i("linc","port: txt1 is "+editText1.getText());
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        Log.i("linc", "onSaveInstanceState(Bundle)");
        super.onSaveInstanceState(outState);

        outState.putString(TEXT_ONE, ""+editText1.getTag(R.id.tag_first));//avoid null point
        outState.putString(TEXT_TWO,""+editText2.getContentDescription());
        Log.i("linc","editText1 to string "+editText1.toString());
        Log.i("linc","editText2 to string "+editText2.toString());
        Log.i("linc","editText3 to string "+editText3.getId());
        outState.putString(""+editText3.getId(),""+editText3.getContentDescription());
//        outState.putSerializable();//object
    }
}
